import sqlite3
from random import randint


class Client:
    i = 0

    def __init__(self):
        self.__client_ID = 0  # 顾客编号（无序随机）
        self.__client_arrive_time_hour = 0  # 顾客到达银行的时间（小时）
        self.__client_arrive_time_minutes = 0  # 顾客到达银行的时间（分钟）
        self.__client_arrive_time = 0  # 顾客到达银行的总时间（分钟）
        self.__business_name_str = ''  # 业务种类(数据库)
        self.__business_name = 0  # 业务种类
        self.__business_name_time = 0  # 业务办理的需要时间
        self.randomClient()

    # 构造函数调用的随机创建顾客的方法
    def randomClient(self):
        self.__client_ID = randint(1000, 10000)
        self.__business_name = randint(0, 3)  # 任何一个客户的办理业务为业务0~3
        if self.__business_name == 0:
            self.__business_name_time = 5
        elif self.__business_name == 1:
            self.__business_name_time = 15
        elif self.__business_name == 2:
            self.__business_name_time = 30
        elif self.__business_name == 3:
            self.__business_name_time = 60
        self.__client_arrive_time_hour = randint(0, 24)  # 顾客到达银行的时间（0~24小时）
        self.__client_arrive_time_minutes = randint(1, 59)  # 顾客到达银行的时间（1~59分钟）
        self.__client_arrive_time = self.__client_arrive_time_hour * 60 + self.__client_arrive_time_minutes

    # 打印客户信息
    def showClientInfor(self):
        print("----------------------------------------------------------------")
        print(" 顾客到来，当前到来的顾客信息为：")
        print("客户ID:", self.__client_ID, "客户到达时间：", self.__client_arrive_time_hour, "时",
              self.__client_arrive_time_minutes, "分")
        if self.__business_name == 0:
            self.__business_name_str = "存款"  # 设置此时该顾客办理业务
        elif self.__business_name == 1:
            self.__business_name_str = "取款"  # 设置此时该顾客办理业务
        elif self.__business_name == 2:
            self.__business_name_str = "挂失"  # 设置此时该顾客办理业务
        elif self.__business_name == 3:
            self.__business_name_str = "还贷"  # 设置此时该顾客办理业务
        print("----------------------------------------------------------------")

        # -----------------------------------数据库-------------------------------------

        Bankdb = sqlite3.connect("Bankdb.db")
        cur = Bankdb.cursor()
        # cur.execute("create table custom(id integer(4),arrivetime varchar(10),service varchar（10)")
        cur.execute("insert into custom values(?,?,?)",
                    (self.__client_ID, self.__client_arrive_time, self.__business_name_str))
        cur.execute("select * from custom ")

        # for item in cur.fetchall():
        #     print(item)

        # cur.execute('delete from custom')
        Bankdb.commit()

        Bankdb.close()
        # -----------------------------------------------------------------------------------
        # --------------------------------treeview窗口---------------------
        # win = tkinter.Tk()
        # win.title("gui")
        # win.geometry("600x600+200+200")
        # tree = ttk.Treeview(win)
        # tree.pack()
        # tree["columns"] = ('id', '办理总时间', '业务种类')
        # tree.column('id', width=120)
        # tree.column('办理总时间', width=200)
        # tree.column('业务种类', width=200)
        #
        # tree.heading('id', text='id——name')
        # tree.heading('办理总时间', text='办理总时间——name')
        # tree.heading('业务种类', text='业务种类——name')
        # tree.insert("", 0, text='line',values = (self.__client_ID,self.__client_arrive_time,self.__business_name_str))
        # win.mainloop()

    def getClientArriveTime(self):
        return self.__client_arrive_time

    def getClientID(self):
        return self.__client_ID

    def getClientArriveTimeHour(self):
        return self.__client_arrive_time_hour

    def getClientArriveTimeMinutes(self):
        return self.__client_arrive_time_minutes

    def getClientBusinessName(self):
        return self.__business_name

    def getClientBusinessNameTime(self):
        return self.__business_name_time

    # 各种属性的set方法
    def setClientArriveTime(self, a=0):
        self.__client_arrive_time = a

    def setClientID(self, a):
        self.__client_ID = a

    def setClientArriveTimeHour(self, a):
        self.__client_arrive_time_hour = a

    def setClientArriveTimeMinutes(self, a):
        self.__client_arrive_time_minutes = a

    def setClientBusinessName(self, a):
        self.__business_name = a

    def setClientBusinessNameTime(self, a):
        self.__business_name_time = a


# ------------------------------------------等待队列--------------------------------------------------
'''
用单链表表示的链式队列特别适合于数据元素变动比较大的情形，
而且不存在队列满且产生溢出的问题。另外，假如程序中要使用多个队列，与多个栈的情形一样，
最好使用链式队列，这样就不会出现存储分配不合理和“溢出”的问题。
'''


class Person:
    def __init__(self):
        self.client = Client()  # 数据域：顾客类的对象
        self.next = 0


class LinkedQueue:
    def __init__(self):
        self.joinqueue = []

    def joinQueue(self, client, open_time, closed_time):
        for i in range(len(client)):  # 将时间符合的加入排队中
            if closed_time * 60 > client[i].getClientArriveTime() > open_time * 60:
                self.joinqueue.append(client[i])
        # return self.joinqueue

    def deleteQueue(self):
        return self.joinqueue.pop(0)

    def empty(self):
        return self.joinqueue == []

    def getQueueLength(self):
        return len(self.joinqueue)

    def getElumn(self):
        pass


class Bank:
    def __init__(self, open_time, closed_time):
        self.__bank_open_time = open_time  # 银行开始营业时间（小时）
        self.__bank_closed_time = closed_time  # 银行结束营业时间（小时）
        self.__bank_all_time = self.__bank_closed_time * 60 - self.__bank_open_time * 60  # 银行总营业时间（分钟） = (银行结束营业时间（小时）- 银行开始营业时间（小时） * 60)
        self.__business_start_time = self.__bank_open_time * 60  # 开始办理业务时间（分钟）
        self.__business_end_time = self.__bank_open_time * 60  # 上一个业务办理的结束时间（分钟）
        self.__save_money_num = 0  # 存款业务办理次数
        self.__get_money_num = 0  # 取款业务办理次数
        self.__report_loss_of_num = 0  # 挂失业务办理次数
        self.__refund_num = 0  # 还款业务办理所次数
        self.__client_stay_bank_time = 0  # 顾客逗留银行的时间（分钟）
        self.__windows_empty = False  # 窗口是否为空（true：空、false：不空）

    # 各属性的get方法
    def getBankAllTime(self):  # 得到总时间常用
        return self.__bank_all_time

    def getWindowsEmpty(self):  # 得到窗口是否为空常用
        return self.__windows_empty

    def getClientStayBankTime(self):  # 得到客户在该窗口的逗留的总时间
        return self.__client_stay_bank_time

    def getSaveMoneyNum(self):
        return self.__save_money_num

    def getGetMoneyNum(self):
        return self.__get_money_num

    def getReportLossOfNum(self):
        return self.__report_loss_of_num

    def getRefundNum(self):
        return self.__refund_num

    def getBankOpenTime(self):
        return self.__bank_open_time

    def getBankClosedTime(self):
        return self.__bank_closed_time

    def getBusinessStartTime(self):
        return self.__business_start_time

    def getbusinessEndTime(self):
        return self.__business_end_time

    def getTotalNums(self):
        return self.getSaveMoneyNum() + self.getGetMoneyNum() + self.getReportLossOfNum() + self.getRefundNum()

    # 各种属性的set方法
    def setBankAllTime(self, time):  # 设置总时间常用
        self.__bank_all_time = time

    def setWindowsEmpty(self, status):  # 设置窗口是否为空常用
        self.__windows_empty = status

    def setClientStayBankTime(self, time):  # 设置客户在该窗口逗留的总时间
        self.__client_stay_bank_time = time

    def setSaveMoneyNum(self):
        self.__save_money_num += 1

    def setGetMoneyNum(self):
        self.__get_money_num += 1

    def setReportLossOfNum(self):
        self.__report_loss_of_num += 1

    def setRefundNum(self):
        self.__refund_num += 1

    def setBankOpenTime(self, time):
        self.__bank_open_time = time

    def setBankClosedTime(self, time):
        self.__bank_closed_time = time

    def setBusinessStartTime(self, time):
        self.__business_start_time = time

    def setbusinessEndTime(self, time):
        self.__business_end_time = time

# a = list(Client() for i in range(100))
# print(a[0].getClientArriveTime())
# print(a[1].getClientArriveTime())
#         # self.next = Person()
# a = Person()
# b = a.client.getClientID()
# print(b)

# a = Client()
# a.randomClient()
